Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect cf-url-rewriter to fetch respective index.json file #378

Merged
merged 3 commits into from
Mar 4, 2024

Conversation

Divyaasm
Copy link
Collaborator

Description

Redirect cf-url-rewriter to fetch latest buildnumber from the index.json file corresponding to the given distribution type in the url

Issues Resolved

opensearch-project/opensearch-build#4166

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@rishabh6788
Copy link
Collaborator

Question: Have you verified downloads with existing/older artifacts?

@Divyaasm
Copy link
Collaborator Author

Question: Have you verified downloads with existing/older artifacts?

Yes Rishabh, this change is compatible new folder structure index. uploadIndexFile library will now store the latest build number of a distribution in it's respective sub-folder inside index folder

@tianleh
Copy link
Member

tianleh commented Dec 27, 2023

My original PR when implementing the latest feature came with unit tests. Looks like the test files were not moved over from build repo to this ci repo.

Could you make the move and update the test files?

@Divyaasm

@tianleh
Copy link
Member

tianleh commented Dec 27, 2023

Yes Rishabh, this change is compatible new folder structure index. uploadIndexFile library will now store the latest build number of a distribution in it's respective sub-folder inside index folder

Have a similar question as @rishabh6788 What about the latest url for an old version, e.g 1.2.0 which may not have a recent build and thus still have an old structure?

@Divyaasm
Copy link
Collaborator Author

Divyaasm commented Dec 27, 2023

Yes Rishabh, this change is compatible new folder structure index. uploadIndexFile library will now store the latest build number of a distribution in it's respective sub-folder inside index folder

Have a similar question as @rishabh6788 What about the latest url for an old version, e.g 1.2.0 which may not have a recent build and thus still have an old structure?

The index file folder is updated in the corresponding build jenkins workflow when triggered. Since we do not touch the older versions they remain the same. And coming to the latest running jobs the path for index.json is present in index folder in s3 bucket.

@@ -66,3 +73,12 @@ function errorResponse() {
statusDescription: 'Not found',
};
}

function getIndexFilePath(versionNumber: string, urisplit: string[]): string {
if (versionNumber <= "2111") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the release history https://github.com/opensearch-project/opensearch-build/releases , I see that we are still releasing 1.3.x If we have a 1.3.15 release, looks like the logic here will still use the old index.json path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the requested changes. Can you please review again @tianleh. Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented #378 (comment)

Comment on lines 22 to 24
const urisplit = request.uri.split("/latest");
const index = urisplit[0].lastIndexOf("/");
const versionNumber = urisplit[0].substring(index + 1).replace(/\./g, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nonblocking: all these variables are used for getIndexFilePath and not in the main function. Better to move them into the sub function getIndexFilePath

Comment on lines 80 to 82
(major == 1 && minor == 3 && patch >= 15) ||
(major == 2 && minor >= 12) ||
(major >= 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed a tricky problem. The latest patch release for 2.11 is 2.11.1 I think it is theoretically possible to have a newer patch release, say 2.11.2. Then this logic will fail. The same thing will happen to all existing 2.x releases < 2.12

So we have several options here.

Option 1: extend your current comparison logic to cover all 2.x versions. This will be a long list.

Option 2: back fill S3 buckets for all released versions with the new structure. Then you can remove the comparison. This can simplify the code but require tedious manual effort.

Option 3: always run httpsGet against the new url. If returned empty, then fall back to the old url. Then you do not need to backfill S3 buckets and also do not need to maintain a long comparison list.

I would prefer Option 3 for simplicity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to get the semver try using https://www.npmjs.com/package/semver library.

const data: any = await httpsGet('https://' + request.headers.host[0].value + indexUri);
const latestIndexUri = request.uri.replace(/\/latest\/.*/, '/index' + latestPath + '/index.json');

try{
Copy link
Member

@tianleh tianleh Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing indentation whitespace

Comment on lines 33 to 35
try{
const oldIndexUri = request.uri.replace(/\/latest\/.*/, '/index.json');
const indexData: any = await httpsGet('https://' + request.headers.host[0].value + oldIndexUri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing indentation. Please format the code

Comment on lines 22 to 46
const latestPath = request.uri.split("/latest")[1].split("/").slice(0, 4).join("/");

try {
const data: any = await httpsGet('https://' + request.headers.host[0].value + indexUri);
const latestIndexUri = request.uri.replace(/\/latest\/.*/, '/index' + latestPath + '/index.json');

try{
const data: any = await httpsGet('https://' + request.headers.host[0].value + latestIndexUri);

if (data && data.latest) {
callback(null, redirectResponse(request, data.latest));
} else {
callback(null, errorResponse());
}
} catch (e) {
console.log(e);
callback(null, errorResponse());
}
try{
const oldIndexUri = request.uri.replace(/\/latest\/.*/, '/index.json');
const indexData: any = await httpsGet('https://' + request.headers.host[0].value + oldIndexUri);

if (indexData && indexData.latest) {
callback(null, redirectResponse(request, indexData.latest));
} else {
callback(null, errorResponse());
}
} catch (e) {
console.log(e);
callback(null, errorResponse());
}
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify the code by extracting common logic to a sub function. Below is the skeleton.


function getLatest(url) {

  try {
    const data: any = await httpsGet(url);

    if (data && data.latest) {
      return data.latest;
    }

    return "";
  } catch (e) {
    console.log(e);
    return "";
  }

}

function handler(() {

  ...
  const latestFromNewUrl = getLatest(newUrl);

  if (latestFromNewUrl) {
    callback(null, redirectResponse(request, latest));
  } else {
    const latestFromOldUrl = getLatest(oldUrl);

    if (latestFromOldUrl) {
      callback(null, redirectResponse(request, latestFromOldUrl));
    } else {
      callback(null, errorResponse());
    }

  }

  ...
}

@Divyaasm Divyaasm force-pushed the updateurl branch 2 times, most recently from 12ac168 to c890e0a Compare January 8, 2024 23:25
@rishabh6788
Copy link
Collaborator

@Divyaasm Do we need this now?

@Divyaasm
Copy link
Collaborator Author

@Divyaasm Do we need this now?

This will be used for deploying the the cf-url changes using opensearch-ci repo. I need to make the PR similar to the internal changes. We can have the test cases too here.

@rishabh6788
Copy link
Collaborator

rishabh6788 commented Feb 28, 2024

LGTM! Please fix the merge conflict.

@Divyaasm Divyaasm force-pushed the updateurl branch 6 times, most recently from 24e6c3c to 0552a0a Compare February 28, 2024 23:30
Signed-off-by: Divya Madala <[email protected]>

Update cf-url-rewriter and add testfiles

Signed-off-by: Divya Madala <[email protected]>

add package.json file

Signed-off-by: Divya Madala <[email protected]>

Add changes

Signed-off-by: Divya Madala <[email protected]>

Add changes

Signed-off-by: Divya Madala <[email protected]>

Refactor code

Signed-off-by: Divya Madala <[email protected]>

Add cdn changes to support latest changes

Signed-off-by: Divya Madala <[email protected]>

Resolve merge conflicts

Signed-off-by: Divya Madala <[email protected]>
Signed-off-by: Divya Madala <[email protected]>
@rishabh6788 rishabh6788 merged commit 3b5ece4 into opensearch-project:main Mar 4, 2024
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

4 participants